package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.Uom;
import com.efast.dmom.mes.dao.entity.UomDao;
import com.efast.dmom.mes.entity.PieceWageEntity;
import com.efast.dmom.mes.entity.UomEntity;
import com.efast.dmom.mes.service.PieceWageService;
import com.efast.dmom.mes.service.UomService;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

@Service
public class UomServiceImpl implements UomService{
	
	@Autowired
	private UomDao uomDao;
	
	@Autowired
	private PieceWageService pieceWageService;

	@Override
	public ResultBean verifyUnit(Uom uom) {
		ResultBean resultBean = new ResultBean(true," ");
		UomEntity uomEntity = uom.getInnerData();
		//Verify whether the unit is maintained per unit wage
		List<PieceWageEntity> pieceWageList = pieceWageService.getPieceWageListByUomId(uomEntity.getId());
		if (pieceWageList != null && pieceWageList.size() > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该单位被单件工资所引用，无法删除！");
			return resultBean;
		}
		//Verify whether the unit is maintained spare
		int spareQty = uomDao.getSpareQtyByUomId(uomEntity.getId());
		if (spareQty > 0 ) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该单位被备品备件所引用，无法删除！");
			return resultBean;
		}
		//Verify whether the unit is maintained part
		int partQty = uomDao.getPartQtyByUomName(uomEntity.getUomName());
		if (partQty > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该单位被物料所引用，无法删除！");
			return resultBean;
		}
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean deleteUnit(Uom uom) {
		ResultBean resultBean = new ResultBean(true," ");
		UomEntity uomEntity = uom.getInnerData();
		uomEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		uomEntity.setLastModifier(SpringWebUtils.getRequestUser());
		uomEntity.setRowStatus(1);
		uomDao.deleteUnit(uomEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功！");
		return resultBean;
	}

	@Override
	public ResultBean uniqueVerifyUom(Uom uom) {
		ResultBean resultBean = new ResultBean(true," ");
		UomEntity uomEntity = uomDao.uniqueVerifyUom(uom.getInnerData().getUomName());
		if (uomEntity != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("单位名称重复！");
			return resultBean;
		}
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean save(Uom uom) {
		ResultBean resultBean = new ResultBean(true," ");
		UomEntity uomEntity = uom.getInnerData();
		if (uomEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的信息不能为空！");
			return resultBean;
		}
		if (StringUtil.isEmpty(uomEntity.getUomName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("单位名称不能为空！");
			return resultBean;
		}
		UomEntity entity = uomDao.uniqueVerifyUom(uomEntity.getUomName());
		if (entity != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("单位名称不唯一！");
			return resultBean;
		}
		uomEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
		uomEntity.setCreationTime(TimeUtil.getLocalDateTime());
		uomEntity.setCreator(SpringWebUtils.getRequestUser());
		uomEntity.setRowStatus(0);
		uomDao.save(uomEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功");
		return resultBean;
	}

	@Override
	public ResultBean update(Uom uom) {
		ResultBean resultBean = new ResultBean(true," ");
		UomEntity uomEntity = uom.getInnerData();
		if (uomEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的信息不能为空！");
			return resultBean;
		}
		if (StringUtil.isEmpty(uomEntity.getUomName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("单位名称不能为空！");
			return resultBean;
		}
		UomEntity entity = uomDao.getUomEntityById(uomEntity.getId());
		if (!entity.getUomName().equals(uomEntity.getUomName())) {
			//Verify whether the unit is maintained per unit wage
			List<PieceWageEntity> pieceWageList = pieceWageService.getPieceWageListByUomId(uomEntity.getId());
			if (pieceWageList != null && pieceWageList.size() > 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("该单位被单件工资所引用,不可修改单位名称！");
				return resultBean;
			}
			//Verify whether the unit is maintained spare
			int spareQty = uomDao.getSpareQtyByUomId(uomEntity.getId());
			if (spareQty > 0 ) {
				resultBean.setSuccess(false);
				resultBean.setMessage("该单位被备品备件所引用，不可修改单位名称！");
				return resultBean;
			}
			//Verify whether the unit is maintained part
			int partQty = uomDao.getPartQtyByUomName(uomEntity.getUomName());
			if (partQty > 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("该单位被物料所引用，不可修改单位名称！");
				return resultBean;
			}
			UomEntity repeatEntity = uomDao.uniqueVerifyUom(uomEntity.getUomName());
			if (repeatEntity != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("单位名称不唯一！");
				return resultBean;
			}
		}
		uomEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		uomEntity.setLastModifier(SpringWebUtils.getRequestUser());
		uomDao.update(uomEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("修改成功");
		return resultBean;
	}

}
